#include "CTS.h"

CTS::CTS(const char* chArray)
{
	memcpy(m_data, chArray, 188);
	_parseTsHeader();
}

CTS::~CTS(void)
{
}

const char* CTS::getPayload()
{
	return m_data + m_headerLength;
}
unsigned int CTS::getHeaderLength()
{
	return m_headerLength;
}
unsigned int CTS::getPayloadLength()
{
	return 188-m_headerLength;
}
char* CTS::getData()
{
	return m_data;
}
unsigned int CTS::getContinuityCounter()
{
	return m_continuity_counter;
}
unsigned int CTS::getPID()
{
	return m_PID;
}
unsigned int CTS::getPayloadStartIndicator()
{
	return m_payload_unit_start_indicator;
}
unsigned int CTS::getAdaptationFieldControl()
{
	return m_adaptation_field_control;
}


void CTS::_parseTsHeader()
{
	m_PID = (unsigned int)m_data[2] & 0xff;
	m_payload_unit_start_indicator  = ((unsigned int)m_data[1] & 0x40)/64;
	m_continuity_counter = ((unsigned int)m_data[3] & 0xff) & 0xf;
	m_adaptation_field_control = ((unsigned int)m_data[3] & 0x30)/16;

	m_headerLength= 4;
	if (m_adaptation_field_control==2 || m_adaptation_field_control==3)
	{
		int adaptation_field_length = m_data[4];
		m_headerLength=m_headerLength+adaptation_field_length + 1;
	}
}


bool CTS::isNextTo(int ContinuityCounter){
	return (m_continuity_counter == ContinuityCounter + 1) ||
		((m_continuity_counter==0)&&(ContinuityCounter==15));
}

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void CTS::setError(){
  int iSecret = rand() % ERROR_PROBABILITY + 1;
  if (iSecret == 2)
  {
	  m_data[0] = 99;
  }
}
